let jdate = {
	sdiff: (time) => {
		time = time * 1000;
		let minute = 1000 * 60;
		let hour = minute * 60;
		let day = hour * 24;
		let halfamonth = day * 15;
		let month = day * 30;
		let now = new Date().getTime();

		let tdate = new Date(time);
		let tym = tdate.getFullYear() + '-' + jdate.formatTNum(tdate.getMonth() + 1);
		let tymd = tym + '-' + jdate.formatTNum(tdate.getDate());
		let tmd = jdate.formatTNum(tdate.getMonth() + 1) + '-' + jdate.formatTNum(tdate.getDate());
		let thi = jdate.formatTNum(tdate.getHours()) + ':' + jdate.formatTNum(tdate.getMinutes());

		let diffValue = now - time;
		if (diffValue < 0) return thi;

		let monthC = diffValue / month;
		let weekC = diffValue / (7 * day);
		let dayC = diffValue / day;
		let hourC = diffValue / hour;
		let minC = diffValue / minute;

		if (monthC > 6) return tym;
		if (monthC >= 1) return tmd;
		if (weekC >= 1) return tmd;

		if (dayC == 1) return '昨天';
		if (dayC == 2) return '前天';
		if (dayC >= 1) return tmd;

		if (hourC >= 1) return thi;
		if (minC >= 1) return thi;

		return thi;
	},
	diff: (time, otime) => {
		time = time * 1000;
		let minute = 1000 * 60;
		let hour = minute * 60;
		let day = hour * 24;
		let halfamonth = day * 15;
		let month = day * 30;
		let now = new Date().getTime();

		let tdate = new Date(time);
		let tym = tdate.getFullYear() + '-' + jdate.formatTNum(tdate.getMonth() + 1);
		let tymd = tym + '-' + jdate.formatTNum(tdate.getDate());
		let tmd = jdate.formatTNum(tdate.getMonth() + 1) + '-' + jdate.formatTNum(tdate.getDate());
		let thi = jdate.formatTNum(tdate.getHours()) + ':' + jdate.formatTNum(tdate.getMinutes());

		let diffValue = now - time;
		if (diffValue < 0) return tymd;

		let monthC = diffValue / month;
		let weekC = diffValue / (7 * day);
		let dayC = diffValue / day;
		let hourC = diffValue / hour;
		let minC = diffValue / minute;


		if (monthC >= 1) {
			if (monthC > 12) return tym;
			return tmd;
		}

		//仅显示日期，不显示xx前
		if (otime) {
			if (weekC >= 1) return tmd;
			if (dayC >= 1) return tmd;
			if (hourC >= 1) return thi;
			if (minC >= 1) return thi;

			return thi;
		}

		if (weekC >= 1) return parseInt(weekC) + '周前';
		if (dayC >= 1) return parseInt(dayC) + '天前';
		if (hourC >= 1) return parseInt(hourC) + '小时前';
		if (minC >= 1) return parseInt(minC) + '分钟前';

		return '刚刚';
	},

	time: () => {
		return parseInt(new Date().getTime() / 1000, 10);
	},

	now: () => {
		return jdate.time();
	},

	format: (time, format) => {
		if (!format) format = 'Y-m-d H:i';
		time = time * 1000;
		let tdate = new Date(time);

		let y = tdate.getFullYear();
		let m = jdate.formatTNum(tdate.getMonth() + 1);
		let d = jdate.formatTNum(tdate.getDate());
		let h = jdate.formatTNum(tdate.getHours());
		let i = jdate.formatTNum(tdate.getMinutes());
		let s = jdate.formatTNum(tdate.getSeconds());
		let w = tdate.getDay();
		
		let ws = ['一','二','三','四','五','六','日'];
		
		return format.replace('Y', y).replace('m', m).replace('d', d).replace('H', h).replace('i', i).replace('s', s).replace('w', ws[w]);
	},

	formatTNum: num => {
		if (num < 10) return '0' + num;
		return num;
	},

	datetimeToTime(datetime) {
		var tmp_datetime = datetime.replace(/:/g, '-');
		tmp_datetime = tmp_datetime.replace(/ /g, '-');
		var arr = tmp_datetime.split("-");
		var now = new Date(Date.UTC(arr[0], arr[1] - 1, arr[2], arr[3] - 8, arr[4], arr[5]));
		return parseInt(now.getTime() / 1000);
	}

};


module.exports = jdate;
